package com.ys.peony.ui.view;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.ys.peony.R;
import com.ys.peony.utils.CommonUtils;
import com.ys.peony.utils.PixelUtils;
import com.ys.peony.utils.ToastUtils;
import com.ys.peony.utils.YsUtil;

/**
 * 描述：
 * Created by yukang on 2017/9/12.
 */

public class IdCardDialog implements View.OnClickListener {
    Dialog mDialog;
    private Context mContext;
    private Window mWindow;
    private MyClickListener mClickListener;
    EditText et_name;
    EditText et_idcard;

    public IdCardDialog(Context context, MyClickListener listener, String name, String idCard) {
        mContext = context;
        mClickListener = listener;
        init(name, idCard);
    }

    private void init(String name, String idCard) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.view_dialog_idcard, null, false);
        mDialog = new Dialog(mContext, R.style.dialog);
        mDialog.setContentView(view);
        mDialog.setCanceledOnTouchOutside(false);
        mWindow = mDialog.getWindow();
        mWindow.setGravity(Gravity.CENTER);
        mWindow.setLayout(YsUtil.getScreenWidth(mContext) - PixelUtils.dp2px(50), mWindow.getAttributes().height);
        et_name = (EditText) view.findViewById(R.id.et_name);
        et_idcard = (EditText) view.findViewById(R.id.et_idcard);
        ImageView delete = (ImageView) view.findViewById(R.id.delete);
        et_name.setText(name);
        et_name.setSelection(et_name.getText().length());
        et_idcard.setText(idCard);
        Button submit = (Button) view.findViewById(R.id.submit);
        submit.setOnClickListener(this);
        delete.setOnClickListener(this);
        mDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialogInterface) {
                mClickListener.onClick(null,null);
            }
        });
    }


    public void show() {
        if (mDialog != null) {
            mDialog.show();
        }
    }

    public void dismiss() {
        mDialog.dismiss();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.submit:
                dismiss();
                String name = et_name.getText().toString().trim();
                String idcard = et_idcard.getText().toString().trim();
                if (TextUtils.isEmpty(name)) {
                    ToastUtils.showToast("请输入姓名");
                } else if (!CommonUtils.isIdentityCard(idcard)) {
                    ToastUtils.showToast("请输入正确身份证号");
                } else {
                    if(mClickListener != null) {
                        mClickListener.onClick(name, idcard);
                    }
                }
                break;
            case R.id.delete:
                dismiss();
                break;
        }
    }

    public interface MyClickListener {
        void onClick(String name, String idCard);
    }
}
